//
//  UIBarButtonItem+QSExtension.swift
//  TestSwift
//
//  Created by Song on 2018/3/2.
//  Copyright © 2018年 Song. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    /// 创建图片的BtnItem
    ///
    /// - Parameters:
    ///   - imgName: 图片名
    ///   - highlightImgName: 高亮图片名
    ///   - target: 代理对象
    ///   - action: 执行操作
    class func qs_creatImgBtnItem(imgName: String, highlightImgName: String, target: Any, action: Selector) -> UIBarButtonItem {
        let btn = UIButton.init(type: UIButtonType.custom)
        btn.setImage(UIImage.init(named: imgName), for: UIControlState.normal)
        btn.setImage(UIImage.init(named: highlightImgName), for: UIControlState.highlighted)
        btn.sizeToFit()
        
        btn.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        
        return UIBarButtonItem.init(customView: btn)
    }
    
    /// 创建文字的BtnItem
    ///
    /// - Parameters:
    ///   - title: 文字
    ///   - color: 文字颜色
    ///   - font: 文字字体
    ///   - target: 代理对象
    ///   - action: 执行操作
    class func qs_creatTitleBtnItem(title: String, color: UIColor = .black, font: UIFont = UIFont.systemFont(ofSize: 13.0), target: Any, action: Selector) -> UIBarButtonItem {
        let barBtn = UIBarButtonItem.init(title: title, style: UIBarButtonItem.Style.plain, target: target, action: action)
        barBtn.tintColor = color
        
        let attributes = [NSAttributedString.Key.font: font]
        barBtn.setTitleTextAttributes(attributes, for: .normal)
        barBtn.setTitleTextAttributes(attributes, for: .highlighted)
        barBtn.setTitleTextAttributes(attributes, for: .selected)
        barBtn.setTitleTextAttributes(attributes, for: .disabled)
        
        return barBtn
    }
    
    /// 自定义按钮的BtnItem
    ///
    /// - Parameters:
    ///   - title: 文字
    ///   - img: 图片
    ///   - titleColor: 文字颜色
    ///   - titleFont: 文字字体
    ///   - target: 代理对象
    ///   - action: 执行操作
    class func qs_creatTitleBtnItem(title: String = "", selTitle: String = "", disTitle: String = "", img: String = "", selImg: String = "", disImg: String = "", titleColor: UIColor = .black, selTitleColor: UIColor = .black, disTitleColor: UIColor = .black, titleFont: UIFont = UIFont.systemFont(ofSize: 15.0), target: Any, action: Selector) -> UIBarButtonItem {
        let btnImg = UIImage.init(named: img)
        let btnWidth = self.qs_obtainTextWidthWith(text: title, font: titleFont, height: 20.0) + (btnImg?.size.width ?? 0.0)
        let btnHeight = btnImg?.size.width ?? 25.0
        
        let btn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: btnWidth, height: btnHeight))
        btn.setImage(UIImage.init(named: img), for: .normal)
        btn.setImage(UIImage.init(named: selImg), for: .selected)
        btn.setImage(UIImage.init(named: disImg), for: .disabled)
        btn.setTitle(title, for: .normal)
        btn.setTitle(selTitle, for: .selected)
        btn.setTitle(disTitle, for: .disabled)
        btn.setTitleColor(titleColor, for: .normal)
        btn.setTitleColor(selTitleColor, for: .selected)
        btn.setTitleColor(disTitleColor, for: .disabled)
        btn.titleLabel?.font = titleFont
        btn.addTarget(self, action: action, for: .touchUpInside)
        
        let barBtn = UIBarButtonItem.init(customView: btn)
        return barBtn
    }
    
    /// 获取字符串文字的宽度
    ///
    /// - Parameters:
    ///   - text: 字符串
    ///   - font: 字符串字体
    ///   - height: 高度
    private class func qs_obtainTextWidthWith(text: String, font : UIFont, height : CGFloat) -> CGFloat {
        let attributes = [NSAttributedStringKey.font:font] //设置字体大小
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        
        let rect : CGRect = text.boundingRect(with: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: height), options: option, attributes: attributes, context: nil)
        
        return rect.width
    }
}
